﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Linq.Dynamic;
using System.Net;
using System.Text;
using FaceSharp.Api;
using FaceSharp.Api.Types;
using NUnit.Framework;
using Newtonsoft.Json;
using FaceSharp.MVC.Code.Extensions;
using Newtonsoft.Json.Linq;
using RestSharp;

namespace FaceSharp.MVC.Tests
{
    [TestFixture]
    public class LiveTests
    {
        // Obtain Access token from http://developers.facebook.com/tools/explorer
        public const string FacebookAccessToken = "AAACAg27fvkkBAC8zUhYlNKBqjG8jIEnZAXsd4eazD0lbU9q4m8u4ZC6ILAvTTyiZBb9YkM1GaZCKKkrbhZBq5Tf9T7nNbffQEH0WVnpUmuwZDZD";

        [TestFixtureSetUp]
        public void TestSetup()
        {

        }

        
        public class SimpleUser
        {
            public string Name { get; set; }
            public long Id { get; set; }
            public IList<Like> Likes { get; set; }
        }

        public SimpleUser ParseUser(JToken jObject)
        {
            var likes = new List<Like>();
            var simpleUser = new SimpleUser()
            {
                Id = jObject["id"].Value<long>(),
                Name = jObject["name"].Value<string>()
            };
            var likeData = ((JToken)jObject["likes"]);
            if(likeData != null)
            {
                var jData = (JArray)likeData["data"];
                if (jData != null)
                {
                    likes = (from like in jData
                             select new Like()
                             {
                                 Id = long.Parse((string)like["id"]),
                                 Name = (string)like["name"],
                                 Category = (string)like["category"],
                                 CreatedTime = (string)like["created_time"]
                             }).ToList();
                }
            }
            simpleUser.Likes = likes;
            return simpleUser;
        }

        [Test] 
        public void NbaFans()
        {
            var fbApi = new FacebookApi(FacebookAccessToken);
            var ac = new FaceSharp.MVC.Areas.FacebookApp.Controllers.AdvancedController();
            var results = ac.NbaFanData(fbApi);
            Assert.That(results.Length,Is.GreaterThan(0));
        }

        [Test]
        public void NbaFanSummary()
        {
            var fbApi = new FacebookApi(FacebookAccessToken);
            var ac = new FaceSharp.MVC.Areas.FacebookApp.Controllers.AdvancedController();
            var results = ac.NbaFanSummary(fbApi);
            Assert.That(results.Count(),Is.GreaterThan(0));
        }
        [Test]
        public void DeteteMafiaWarsLike()
        {
            //91644600771/likes/10979261223
            var fbApi = new FacebookApi(FacebookAccessToken);
            var fbCore = new Code.FacebookCore();
            fbCore.AccessToken = FacebookAccessToken;
            var ggc = new FaceSharp.Api.GraphApi.GenericGraphClient(fbCore);
            string results = null;
            RestClient client;
            RestRequest request;
            ggc.GenericGraphRequest("91644600771/likes/10979261223", null, out client, out request, Method.DELETE);
            var response = client.Execute(request);
            Trace.Write(response.ResponseStatus);
            Trace.Write(response.ErrorMessage);


            Debug.Print(response.Content);
            //  Assert.That(response.StatusCode,Is.Not.GreaterThan(400));
        }
        

        [Test]
        public void FriendsLikes()
        {
            var fbApi = new FacebookApi(FacebookAccessToken);

            // me/friends/?fields=name,idlikes
            var results = fbApi.GraphApi.MakeGraphRequest("me", "friends", new List<string> { "name,id,likes" });
            var parsedResults = ((JArray) results["data"]).Select(ParseUser).ToList();

            var myResults = parsedResults
                .SelectMany(x => x.Likes)
                .GroupBy(x => x.Id)
                .Select(x => new {id = x.Key, count = x.Count()})
                .OrderByDescending(x => x.count)
                .ToList();

            Assert.That(myResults.Count, Is.GreaterThan(0));
        }

        [Test]
        public void Friends()
        {
            var fbApi = new FacebookApi(FacebookAccessToken);
            var friends = fbApi.User.GetFriends();
            Assert.That(friends.Count(), Is.GreaterThan(0));
        }
    }
}
